import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/request/facility_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_height_widget.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:core_package/core_package.dart';

class CutDevicePage extends StatefulWidget {
  static const String routeName = '/cutDevicePage';
  final List? data;

  const CutDevicePage({Key? key, this.data}) : super(key: key);

  @override
  _CutDevicePageState createState() => _CutDevicePageState();
}

class _CutDevicePageState extends State<CutDevicePage> {
  int? index;
  List? dataSource;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    if (widget.data == null) {
      FacilityRequest.getUserDeviceListRequest().then((value) {
        if (value != null) {
          var userDeviceListModel = value;
          dataSource = userDeviceListModel.devList!.map((e) => e.devId).toList();
          index = dataSource!.indexOf(UserCacheUil.getString(Configuration.deviceId));
          if (index == -1) index = 0;
          setState(() {});
        }
      });
    } else {
      dataSource = widget.data;
      index = dataSource!.indexOf(UserCacheUil.getString(Configuration.deviceId));
      if (index == -1) index = 0;
    }
  }

  @override
  Widget build(BuildContext context) {
    return BaseNavigationWidget(
      title: '切换设备',
      needScrollView: false,
      actions: dataSource == null
          ? []
          : [
              IconButton(
                  icon: Text(
                    '切换',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.px,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                  onPressed: () {
                    Navigator.pop(context, dataSource![index ?? 0]);
                  })
            ],
      child: buildContent(),
    );
  }

  Widget buildContent() {
    if (dataSource == null) {
      return ProgressDialog.showFirstLoading();
    }
    if (dataSource!.length == 0) {
      return EmptyWidget(msg: '暂无设备');
    }
    return Container(
      color: Colors.white,
      child: ListView.builder(
          padding: EdgeInsets.zero,
          itemCount: dataSource!.length,
          itemBuilder: (ctx, index) {
            return ShowTitleDetailListWidget(
              onTap: () {
                this.index = index;
                setState(() {});
              },
              titleWidget: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    dataSource![index],
                    style: TextStyle(
                      color: MyColors.color_34_34_34,
                      fontWeight: FontWeight.w500,
                      fontSize: MyFont.font16,
                    ),
                  ),
                  Text(
                    UserCacheUil.getString(Configuration.deviceId) == dataSource![index] ? ' (当前设备)' : "",
                    style: TextStyle(
                      color: MyColors.color_246_45_29,
                      fontWeight: FontWeight.w400,
                      fontSize: MyFont.font12,
                    ),
                  ),
                ],
              ),
              icon: Icon(
                Icons.check_circle,
                size: 20.px,
                color: this.index == index ? MyColors.color_246_45_29 : Colors.black12,
              ),
            );
          }),
    );
  }
}
